﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Quail;
using QuailDashboard.View.Converters;
using QuailDashboard.View.Models;
using QuailDashboard.View.QuailServiceReference;

namespace QuailDashboard.View.Data
{
    public class LogEntries
    {
        private readonly ObservableCollection<TestRunTaskLogEntry> _allLogEntries = new ObservableCollection<TestRunTaskLogEntry>();
        private readonly IEnumerable<TestRunTaskLogEntry> _filteredLogs;

        public LogEntries()
        {
            Filter = new FilterModel();

            _filteredLogs = _allLogEntries.Where(data =>
            {
                var statusid = (LogStatus)data.EntryStatusId;

                if (!string.IsNullOrEmpty(Filter.FilterString))
                {
                    if (data.EntryText.IndexOf(Filter.FilterString, StringComparison.CurrentCultureIgnoreCase) < 0)
                    {
                        if (data.Url == null)
                            return false;

                        if (data.Url.IndexOf(Filter.FilterString, StringComparison.CurrentCultureIgnoreCase) < 0)
                            return false;
                    }
                }

                if (!Filter.FilterFailed && !Filter.FilterFailedYSOD && !Filter.FilterPending && !Filter.FilterAsyncPending && !Filter.FilterWarning && !Filter.FilterPassed)
                    return true;

                if (Filter.FilterFailed && statusid == LogStatus.Fail)
                    return true;

                if (Filter.FilterFailedYSOD && statusid == LogStatus.FailYsod)
                    return true;

                if (Filter.FilterAsyncPending && statusid == LogStatus.AsyncPending)
                    return true;

                if (Filter.FilterPending && statusid == LogStatus.Pending)
                    return true;

                if (Filter.FilterPassed && statusid == LogStatus.Pass)
                    return true;

                if (!Filter.FilterWarning)
                    return false;

                // Warning state is a "catch all" for all other states
                switch (statusid)
                {
                    case LogStatus.Pass:
                    case LogStatus.AsyncPending:
                    case LogStatus.Pending:
                    case LogStatus.Fail:
                    case LogStatus.FailYsod:
                        return false;
                    default:
                        return true;
                }
            });
        }

        /// <summary>
        /// Gets the filtered log entries
        /// </summary>
        public IEnumerable<TestRunTaskLogEntry> FilteredLogEntries
        {
            get
            {
                return _filteredLogs;
            }
        }

        /// <summary>
        /// Gets all the log entries for the test
        /// </summary>
        public ObservableCollection<TestRunTaskLogEntry> AllLogEntries
        {
            get
            {
                return _allLogEntries;
            }
        }

        public FilterModel Filter
        {
            get; private set;
        }
    }
}
